﻿using FishSimulation.Model;
using FishSimulation.Views;
using System;
using System.Windows;
using System.Windows.Media.Imaging;

namespace FishSimulation {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        #region Constructors
        public MainWindow() {
            double waterTemp = Repository.Instance.World.Pond.WaterTemp;
            InitializeComponent();

            //sliderDelay.Value = Repository.Instance.World.Delay;
            sliderTemp.Value = waterTemp;
        }
        #endregion

        #region Event Handlers
        private void mnuActors_Click(object sender, RoutedEventArgs e) {
            Window w = Utils.CreateWindow();
            w.Content = new ActorGridView();
            w.ShowDialog();
            this.Bind();
        }

        private void mnuActorTypes_Click(object sender, RoutedEventArgs e) {
            Window w = Utils.CreateWindow();
            w.Content = new ActorTypeGridView();
            w.ShowDialog();
            this.Bind();
        }

        private void mnuExit_Click(object sender, RoutedEventArgs e) {
            if (this.Confirm("Really quit?"))
                App.Current.Shutdown();
        }
        
        private void mnuPlay_Click(object sender, RoutedEventArgs e) {
            if (world.Playing)
            {
                this.Pause();
            }
            else {
                this.Play();
            }            
        }

        private void mnuPond_Click(object sender, RoutedEventArgs e) {
            Window w = Utils.CreateWindow();
            var d = new PondDetailView();
            w.Content = d;
            w.ShowDialog();
            if (!d.Canceled) {
                this.Bind();
            }
        }

        private void mnuReload_Click(object sender, RoutedEventArgs e)
        {
            if (this.Confirm("Really reload?"))
            {
                this.Pause();
                Repository.Instance.Init();
                world.BindPond();
            }
        }

        private void mnuSave_Click(object sender, RoutedEventArgs e) {
            if (this.Confirm("Really save?"))
                Repository.Instance.Save();
        }
        
        private void sliderDelay_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
            if(world != null)
                world.Delay = sliderDelay.Value;
        }

        private void sliderTemp_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
            if(Repository.Instance.World != null && Repository.Instance.World.Pond != null)
                Repository.Instance.World.Pond.WaterTemp = sliderTemp.Value;
        }

        private void world_ViewBaseClicked(ViewBase sender, System.Windows.Input.MouseButtonEventArgs e) {
            var m = sender.GetPropertyGridItem();
            if (m != null) {
                var a = m as Actor;
                if (a != null)
                    Repository.Instance.FillActorType(null, a);
            }
            this.propertyGrid.SelectedObject = m;
            tab.SelectedIndex = 1;
        }
        #endregion

        #region Methods
        private void Bind() {
            this.propertyGrid.SelectedObject = null;
            this.world.BindPond();
        }

        private void Pause()
        {
            world.Playing = false;
            string uri = "pack://application:,,,/Images/play.gif";
            imgPlay.Source = new BitmapImage(new Uri(uri));
        }

        private void Play()
        {
            world.Playing = true;
            string uri = "pack://application:,,,/Images/pause.png";
            imgPlay.Source = new BitmapImage(new Uri(uri));
        }
        #endregion        
    }
}
